package de.fuberlin.inf.alp3.ue07;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RectangularPaint extends JFrame implements MouseMotionListener, MouseListener{

    private static final long serialVersionUID = 1L;

    private RectPanel rectPanel; 
    
    private int x1, x2, y1, y2;
    private Color color = Color.black;

    public RectangularPaint(String title) {
        super(title); 
        setupGui();           
        pack();             
        setVisible(true);   
        rectPanel.reset();
    }

    private void setupGui() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.rectPanel = new RectPanel();
        getContentPane().add(rectPanel, BorderLayout.NORTH);
        
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        
        JPanel southPanel = new JPanel();

        JButton resetButton = new JButton("Reset");
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                rectPanel.reset();
            }
        });
        southPanel.add(resetButton);
        
        JButton blackButton = new JButton("Black");
        blackButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                color = Color.black;
            }
        });
        southPanel.add(blackButton);
        
        JButton yellowButton = new JButton("Yellow");
        yellowButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                color = Color.yellow;
            }
        });
        southPanel.add(yellowButton);
        
        JButton blueButton = new JButton("Blue");
        blueButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                color = Color.blue;
            }
        });
        southPanel.add(blueButton);
        
        JButton redButton = new JButton("Red");
        redButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                color = Color.red;
            }
        });
        southPanel.add(redButton);   

        getContentPane().add(southPanel, BorderLayout.SOUTH);
    }

    public void mousePressed(MouseEvent e) {
        x1 = x2 = this.getMousePosition().x;
        y1 = y2 = this.getMousePosition().y;
    }
    
    public void mouseDragged(MouseEvent arg0) {
        if(this.getMousePosition().x <= x1) {
            x1 = this.getMousePosition().x;
        } else {
            x2 = this.getMousePosition().x;
        }
        if(this.getMousePosition().y <= y1) {
            y1 = this.getMousePosition().y;
        } else {
            y2 = this.getMousePosition().y; 
        }
        rectPanel.drawRect(x1, y1, x2-x1, y2-y1, color);
    }

    public void mouseMoved(MouseEvent e) {  
    }
    public void mouseReleased(MouseEvent e) {
    }   
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }   
    public void mouseClicked(MouseEvent e) {
    }
    
    public static void main(String[] args) {
        RectangularPaint r = new RectangularPaint("Rectangular Paint");
    }
}

class RectPanel extends JPanel {
    
    private static final long serialVersionUID = 1L;
    
    private int x = 0, y = 0, width = getWidth(), height = getHeight();
    private Color color;
    
    RectPanel() {
        setPreferredSize(new Dimension(500, 400));
    }
    
    public void reset() {   
        this.color = Color.white;
        this.width = getWidth();
        this.height = getHeight();
        this.x = 0;
        this.y = 0;
        Graphics g = getGraphics();
        g.clearRect(x, y, width, height); 
        repaint();
    }

    public void drawRect(int x, int y, int width, int height, Color color) {
        this.color = color;
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        repaint();
    }

    public void paint(Graphics g) {
        g.setColor(this.color);
        g.fillRect(x, y, width, height);
    }
}